package com.leyou.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.awt.SunHints;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

  private BrandService brandService;

  public BrandController (BrandService brandService){this.brandService=brandService;};

  /**
   * 根据品牌id集合查询品牌集合
   * @param ids
   * @return
   */
  @GetMapping("/list")
  public ResponseEntity<List<BrandDTO>> listBrandByIds(@RequestParam("ids")List<Long>ids){
      return ResponseEntity.ok(this.brandService.listBrandByIds(ids));
  }

  /**
   * 根据分类id查询品牌
   * @param cid
   * @return
   */
  @GetMapping("/of/category")
  public ResponseEntity<List<BrandDTO>>queryBrandByCategoryId(@RequestParam("id") Long cid){
    return ResponseEntity.ok(this.brandService.queryBrandByCategoryId(cid));
  }

  /**
   * 根据id查询品牌
   * @param bid
   * @return
   */
  @GetMapping("/{id}")
  public ResponseEntity<BrandDTO>queryBrandById(@PathVariable("id") Long bid){
    return ResponseEntity.ok(this.brandService.queryBrandById(bid));
  }

  /**
   * 根据分类id查询品牌
   * @param page
   * @param rows
   * @return
   */

  @GetMapping("/page")
  public ResponseEntity<PageDTO<BrandDTO>>queryPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                    @RequestParam(value = "rows",defaultValue = "5") Integer rows){

    return ResponseEntity.ok(this.brandService.queryPage(page,rows));

  }


//  public ResponseEntity<Void>addBrand(
//          @RequestParam("categoryIds") List<Long> cids,
//          @RequestParam(" letter") Character  letter,
//          @RequestParam("name") String bname,
//          @RequestParam(value = "image",required = false)String image,
//
//  ){}
@PostMapping
  public ResponseEntity<Void>addBrand(BrandDTO brandDTO){

    this.brandService.addBrand(brandDTO);
    return ResponseEntity.status(201).build();
  }

  @PutMapping
  public ResponseEntity<Void>updateBrand(BrandDTO brandDTO){

    this.brandService.updateBrand(brandDTO);
    return ResponseEntity.status(201).build();
  }

//  @DeleteMapping("/{id}")
//  public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
//    // 删除品牌表
//    brandService.removeById(id);
//    // TODO 删除中间表
//    categoryBrandService.remove(
//            new QueryWrapper<CategoryBrand>().eq("brand_id", id));
//    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
//  }


  @DeleteMapping("/{id}")
  public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
    this.brandService.deleteBrandById(id);

    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }
}

